package cn.wxManage.user.service.impl;

import cn.common.common.model.WXUserLoginInfo;
import cn.wxManage.common.util.RedisUtils;
import cn.wxManage.user.dao.UserInfoMapper;
import cn.wxManage.user.dao.UserRoleMapper;
import cn.wxManage.user.model.UserInfo;
import cn.wxManage.user.model.UserRole;
import cn.wxManage.user.service.IWXUserService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import cn.common.common.model.ServerResponse;
import com.google.common.hash.Hashing;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by Zhang on 2018/11/29.
 */
@Service
public class WXUserService implements IWXUserService {

    @Autowired
    HttpServletRequest request;

    @Autowired
    UserInfoMapper userInfoMapper;

    @Autowired
    UserRoleMapper userRoleMapper;

    @Autowired
    RedisUtils redisUtils;

    @Override
    public ServerResponse<?> getWXUserInfo(String code) {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet("https://api.weixin.qq.com/sns/jscode2session?" +
                "appid=wxa3eed48f76f9022a&" +
                "secret=6c9388b1bde653d50a395063489d3d52&" +
                "js_code="+code+"&" +
                "grant_type=authorization_code");
        CloseableHttpResponse httpResponse = null;
        try {
            httpResponse = httpClient.execute(httpGet);
            if (httpResponse.getStatusLine().getStatusCode() == 200){
                HttpEntity httpEntity = httpResponse.getEntity();
                String json = EntityUtils.toString(httpEntity);
                WXUserLoginInfo info = JSONObject.parseObject(json,WXUserLoginInfo.class);
                if (info.getErrcode()!=null && !"0".equals(info.getErrcode())){
                    return ServerResponse.createByErrorMessage(info.getErrmsg());
                }
               UserInfo userInfo = userInfoMapper.getUserInfoByOpenId(info.getOpenid());
                if (userInfo == null){
                    userInfo = new UserInfo();
                    userInfo.setUsername("访客");
                    userInfo.setOppenId(info.getOpenid());
                    userInfoMapper.insertSelective(userInfo);
                    UserRole userRole = new UserRole();
                    userRole.setUserid(userInfo.getUserid());
                    userRole.setRoleId(9);
                    userRoleMapper.insertSelective(userRole);
                }
                userInfo.setRoleId(userRoleMapper.getUserRoleList(userInfo.getUserid()));
                redisUtils.set(info.getSession_key(), JSON.toJSONString(userInfo),1800L);
                Map<String,Object> resultMap = new HashMap<String, Object>(2);
                resultMap.put("key",info.getSession_key());
                resultMap.put("userInfo",userInfo);
                return ServerResponse.createBySuccess(resultMap);
            }else {
                return ServerResponse.createByErrorMessage("连接微信身份认证服务器失败");
            }
        }catch (Exception e){
            return ServerResponse.createByErrorMessage("身份认证错误");
        }
    }
}
